<?php
/**************************************
 *
 * @Repositories 数据层~主要与数据库打交道
 *
 **************************************/
namespace app\common\repositories;

use app\common\model\UserModel;
use app\common\model\CalendarModel;
use think\Model;
use think\db;
use app\common\services\RedisServices;

class ClickRepositories
{
    protected $UserModel;
    protected $CalendarModel;
    protected $RedisServices;
    // 修改用户信息自动过滤字段
    protected $field = ['mood', 'img', 'update_time', 'label', 'is_arti'];
    //点赞自动筛选字段
    protected $clickFile = ['click'];

    public function __construct()
    {
        $this->UserModel = new UserModel();
        $this->CalendarModel = new CalendarModel();
        $this->RedisServices = new RedisServices();
    }

    //用户点赞功能
    public function clickDaily($param)
    {
        $checkData = $this->CalendarModel->unset_data($param, $this->clickFile);
        if ($param['click'] == 1) {
            $data['click'] = -1;
        } elseif ($param['click'] == -1) {
            $data['click'] = 1;
        }
        $msg = $this->CalendarModel->where('id', $param['id'])->update($data);
        if ($msg) {
            return ['whether' => $data['click']];
        } else {
            return 400;
        }
    }

    //文章点赞
    public function setClick($param)
    {
        return $this->RedisServices->setClick($param);
    }

    //专题点赞
    public function setFollow($param)
    {
        return $this->RedisServices->setFollow($param);
    }

    //获取redis点赞量
    public function clickNum($data)
    {
        foreach ($data as $k => &$v) {
            $v['click'] = $this->RedisServices->getClick($v['id']);
            $v['whether'] = $this->RedisServices->isClick($v['id'], $v['user_id']);
        }
        return $data;
    }

    //获取redis关注数
    public function followNum($data)
    {
        foreach ($data as $k => &$v) {
            $v['follow_num'] = $this->RedisServices->getFollow($v['id']);
            if (isset($v['user_id'])) {
                $v['whether'] = $this->RedisServices->isFollow($v['id'], $v['user_id']);
            } else {
                $v['whether'] = -1;
            }
        }
        return $data;
    }


    //获取文章点赞的用户头像
    public function getClickImg($data)
    {
        foreach ($data as $k => &$v) {
            $v['click'] = $this->RedisServices->getClickImg($v['id']);
        }
        return $data;
    }

    //获取关注过的专题
    public function getFollowSp($user_id)
    {
        $data = $this->RedisServices->getFollowSp($user_id);

        if ($data) {
            return $data;
        } else {
            return false;
        }
    }

    //获取关注过的专题
    public function getClickAtr($user_id)
    {
        return $this->RedisServices->getClickAtr($user_id);
    }

    //获取文章点赞的用户头像
    public function getFollowImg($id)
    {
        return $this->RedisServices->getFollowImg($id);
    }

    //文章浏览量
    public function addView($param)
    {
        return $this->RedisServices->addView($param);
    }

    //获取文章浏览量
    public function getView($id)
    {
        return $this->RedisServices->getView($id);
    }

    //获取近期浏览过的文章
    public function getViewArt($user_id)
    {
        return $this->RedisServices->getViewArt($user_id);
    }
}